//
//  OAuthViewController.m
//  coca
//
//  Created by 张帅 on 15/9/27.
//  Copyright © 2015年 ZhangShuai. All rights reserved.
//

#import "OAuthViewController.h"
#import "NetworkTools.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "UserAccountTool.h"
#import "UserAccountViewModel.h"
@interface OAuthViewController ()<UIWebViewDelegate>

@property(nonatomic,strong) UIWebView *webView;

//- (void)loadUserInfoWithAccount:(UserAccount*)account;

@end

@implementation OAuthViewController


-(void)loadView {

    self.view =self.webView;
    self.webView.delegate =self;
    self.title =@"授权";
    
    self.navigationItem.leftBarButtonItem =  [[UIBarButtonItem alloc] initWithTitle:@"注销" style:(UIBarButtonItemStylePlain) target:self action:@selector(logOutcloseClick)];
    
    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:(UIBarButtonItemStylePlain) target:self action:@selector(autofill)];
    

}

- (void) logOutcloseClick{
    
    [SVProgressHUD dismiss];
   
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)autofill{
    
    
    NSString *  count = @"document.getElementById('userId').value = '15692207526';";
    
    NSString *pass= @"document.getElementById('passwd').value = '13612176840';";
   
     NSString *js = [NSString stringWithFormat:@"%@%@",count,pass];
    

   [self.webView stringByEvaluatingJavaScriptFromString:js];
}


- (void)viewDidLoad{
    [super viewDidLoad];
    
    NSURL *url =[[NetworkTools sharedTools] oauthUrl];
    
    NSURLRequest *request =[[NSURLRequest alloc] initWithURL:url];
    
    [self.webView loadRequest:request ];
}



- (void)webViewDidStartLoad:(UIWebView *)webView{

//    [SVProgressHUD dismiss];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
  
//    [SVProgressHUD dismiss];
}
// MARK: - UIWebViewDelegate
// 通常在 iOS 开发中，如果代理方法有 Bool 类型的返回值，返回 true 通常是一切正常

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *urlString = request.URL.absoluteString;
    NSLog(@"%@",urlString);
    if (![urlString hasPrefix:[NetworkTools sharedTools].redirectUri]) {
        
        return YES;
    }
    
    NSLog(@"%@",request.URL.query);
    
    NSString *query = request.URL.query;
    
    if ([query hasPrefix:@"code="]) {
        
        NSRange rang = [query rangeOfString:@"code="];
        
        NSInteger fromIndex = rang.length + rang.location;
        
        NSString *code = [query  substringFromIndex: fromIndex];
        
        [[[UserAccountViewModel sharedUserAccount]loadUserAccountCode:code] subscribeError:^(NSError *error) {
            
            NSLog(@"%@",error);
        } completed:^{
            
            NSLog(@"登录完成!");
            
            [[NSNotificationCenter defaultCenter] postNotificationName:HMSwitchRootViewControllerNotification object:@"main"];
        }];
        
        

        NSLog(@"%@",code);
        
    }else{
        NSLog(@"取消");
    }
    
    return  NO;
}

- (UIWebView *)webView{

    if (_webView==nil) {
        _webView=[[UIWebView alloc] init];
        
    }
    return _webView;
}
@end
